
/*
This file is part of MJIN2.

Copyright (C) 2012-2013 Opensource Game Studio

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#include <mjin2/Common.h>
#include <mjin2/SoundExceptions_p.h>
#include <mjin2/SoundSourceBackground.h>
#include <mjin2/SoundSourceStream.h>

#include <AL/alure.h>

namespace mjin2
{

SoundSourceBackground::SoundSourceBackground() throw () :
    mSource(0),
    mEnabled(false),
    mState(SOUND_SOURCE_STATE_STOPPED)
{
    mSource = new SoundSourceStream();
    mSource->addListener(this);
    setEnabled(true);
}

SoundSourceBackground::~SoundSourceBackground() throw ()
{
    mSource->removeListener(this);
    delete mSource;
}

SOUND_SOURCE_STATE SoundSourceBackground::state() const throw (Exception)
{
    return mState;
}

float SoundSourceBackground::volume() const throw (Exception)
{
    return mSource->volume();
}

bool SoundSourceBackground::isEnabled() const throw ()
{
    return mEnabled;
}

void SoundSourceBackground::setActiveSound(const String &name) throw (Exception)
{
    if (!mEnabled)
        return;
    // Remove it from the queue.
    for (auto it = mSoundsToPlay.begin(); it != mSoundsToPlay.end(); ++it)
        if (*it == name)
        {
            mSoundsToPlay.erase(it);
            break;
        }
    mSoundsToPlay.push_back(name);
    play();
    mSource->setState(SOUND_SOURCE_STATE_PLAYING);
}

void SoundSourceBackground::setEnabled(bool state) throw (Exception)
{
    if (mEnabled == state)
        return;
    mEnabled = state;
    if (!mEnabled)
        mSource->setState(SOUND_SOURCE_STATE_STOPPED);
}

void SoundSourceBackground::setSounds(const StringList &list) throw (Exception)
{
    mSounds = list;
    mSoundsToPlay.clear();
    setState(SOUND_SOURCE_STATE_STOPPED);
}

void SoundSourceBackground::setState(SOUND_SOURCE_STATE state) throw (Exception)
{
    bool proceed = !(mSounds.empty() || !mEnabled);
    if (proceed)
        prepare();
    switch (state)
    {
        case SOUND_SOURCE_STATE_PLAYING:
            // Do not play next melody on unpause.
            if (proceed)
            {
                if (mState == SOUND_SOURCE_STATE_STOPPED ||
                        mSource->state() == SOUND_SOURCE_STATE_STOPPED)
                    play();
            }
            else
                return;
        case SOUND_SOURCE_STATE_STOPPED:
        case SOUND_SOURCE_STATE_PAUSED:
            mSource->setState(state);
            break;
        default:
            MJIN2_SOUND_EXCEPTION(
                MJIN2_STR("Unsupported sound source state: %d", state));
    }
    mState = state;
}

void SoundSourceBackground::setVolume(float value) throw (Exception)
{
    mSource->setVolume(value);
}

void SoundSourceBackground::play() throw (Exception)
{
    mSource->setState(SOUND_SOURCE_STATE_STOPPED);
    String sound = mSoundsToPlay.back();
    mSoundsToPlay.pop_back();
    mSource->setStream(sound.c_str());
}

void SoundSourceBackground::prepare() throw ()
{
    if (!mSoundsToPlay.empty())
        return;
    StringList sounds = mSounds;
    //srand(time(0) * rand());
    while (size_t size = sounds.size())
    {
        u16 id = rand() % size;
        mSoundsToPlay.push_back(sounds[id]);
        sounds.erase(sounds.begin() + id);
    }
}

void SoundSourceBackground::onSoundSourceStateChanged(
    SOUND_SOURCE_STATE state) throw (Exception)
{
    if (mState != SOUND_SOURCE_STATE_PLAYING)
        return;
    if (state == SOUND_SOURCE_STATE_STOPPED)
        setState(SOUND_SOURCE_STATE_PLAYING);
}

} // namespace mjin2
